# # 第一阶段：基础环境和依赖安装
# FROM node:20-alpine AS builder
# RUN npm i -g corepack

# # 设置环境变量
# ENV PNPM_HOME="/pnpm"
# ENV PATH="$PNPM_HOME/bin:$PATH"

# # 设置工作目录
# WORKDIR /app

# # 复制所有源代码（包括workspace包）
# COPY . .

# # 使用Turbo构建，只构建API及其依赖（利用Turbo缓存）
# RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
# RUN pnpm run build --filter=@mj-pro/api...

# RUN echo "Builder Success 🎉"

# # 第五阶段：生产运行环境
# FROM node:20-alpine AS production

# # 设置工作目录
# WORKDIR /app

# # 创建非root用户
# RUN addgroup -g 1001 -S nodejs && \
#     adduser -S nodejs -u 1001

# # 复制构建产物
# COPY --from=builder --chown=nodejs:nodejs /app/apps/api/dist ./apps/api/dist
# COPY --from=builder --chown=nodejs:nodejs /app/apps/api/public ./apps/api/public

# # 复制必要的配置文件
# COPY --from=builder /app/apps/api/package.json ./apps/api/
# COPY --from=builder /app/env.docker ./apps/api/.env

# # 切换到API目录
# WORKDIR /app/apps/api

# # 创建必要的目录
# RUN mkdir -p uploads logs && \
#     chown -R nodejs:nodejs /app

# # 切换到非root用户
# USER nodejs

# # 暴露端口
# EXPOSE 3000

# # 健康检查
# HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
#     CMD node -e "require('http').get('http://localhost:3000/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) })" || exit 1

# # 启动命令
# CMD ["node", "dist/index.cjs"]

FROM node:20-alpine AS production

# 设置工作目录
WORKDIR /app

# 创建非root用户
RUN addgroup -g 1001 -S nodejs && \
    adduser -S nodejs -u 1001

# 复制构建产物
COPY apps/api/dist ./apps/api/dist
COPY apps/api/public ./apps/api/public

# 复制必要的配置文件
COPY apps/api/package.json ./apps/api/
COPY env.docker ./apps/api/.env

# 切换到API目录
WORKDIR /app/apps/api

# 创建必要的目录
RUN mkdir -p uploads logs && \
    chown -R nodejs:nodejs /app

# 切换到非root用户
USER nodejs

# 暴露端口
EXPOSE 3000

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD node -e "require('http').get('http://localhost:3000/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) })" || exit 1

# 启动命令
CMD ["node", "dist/index.cjs"]
